package activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;
import com.example.liuyujia.activity.R;

import util.CommonMethod;
import util.Constants;


/**
 * Created by liuyujia on 2015/3/30.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    private Button register;
    private EditText userName, password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        // init
        initView();
    }

    private void initView(){
        register = (Button) findViewById(R.id.btn_register_sure);
        userName = (EditText) findViewById(R.id.et_name);
        password = (EditText) findViewById(R.id.et_password);

        register.setOnClickListener(this);
    }

    private String getUserName(){
        return userName.getText().toString().trim();
    }

    private String getPassword(){
        return  password.getText().toString().trim();
    }

    private boolean verifyInput(){
        if(getUserName().equals("")){
            CommonMethod.showToastMsg(this, getString(R.string.username_empty));
            return  false;
        }
        else if(getUserName().length() != 11){
            CommonMethod.showToastMsg(this, getString(R.string.username_error));
            return  false;
        }
        else if(getPassword().equals("")){
            CommonMethod.showToastMsg(this, getString(R.string.password_empty));
            return  false;
        }
        if(getUserName().length() < 6){
            CommonMethod.showToastMsg(this, getString(R.string.password_error));
            return  false;
        }

        return  true;
    }

    @Override
    public void onClick(View v) {
         switch (v.getId()){
             case R.id.btn_register_sure:
                 if(verifyInput()){
                    showProgressDialog("");
                     signUp();
                 }
                 break;
             default:
                 break;
         }
    }

    private void signUp(){
        AVUser user = new AVUser();
        user.setUsername(getUserName());
        user.setPassword(getPassword());

        // 其他属性可以像其他AVObject对象一样使用put方法添加
        user.put("phone", getUserName());

        user.signUpInBackground(new SignUpCallback() {
            public void done(AVException e) {
                hideProgressDialog();
                if (e == null) {
                    // successfully
                    CommonMethod.showToastMsg(RegisterActivity.this, getString(R.string.register_success));
                    Intent intent = new Intent();
                    intent.putExtra("phone", getUserName());
                    setResult(RESULT_OK, intent);

                    RegisterActivity.this.finish();
                } else {
                    // failed
                    if(Constants.errorMsg.containsKey(e.getCode())) {
                        CommonMethod.showToastMsg(RegisterActivity.this, Constants.errorMsg.get(e.getCode()));
                    }
                    else
                        CommonMethod.showToastMsg(RegisterActivity.this, getString(R.string.register_failed));

                    return;
                }
            }
        });
    }


}
